﻿using System;
using System.Collections.Generic;
using System.Text;
//using System.Linq;

static class IEnumerableExtensions
{
    public static T Min<T>(this IEnumerable<T> collection)
    {
        dynamic result = null;
        foreach (T item in collection)
        {
            if (result == null)
            {
                result = item;
            }
            else if (result.CompareTo(item) > 0)
            {
                result = item;
            }
        }
        return result;
    }

    public static T Max<T>(this IEnumerable<T> collection)
    {
        dynamic result = null;
        foreach (T item in collection)
        {
            if (result == null)
            {
                result = item;
            }
            else if (result.CompareTo(item) < 0)
            {
                result = item;
            }
        }
        if (result == null)
        {
            throw new ArgumentOutOfRangeException("Collection is empty");
        }
        return result;
    }

    public static T Add<T>(this IEnumerable<T> collection)
    {
        dynamic result = null;
        foreach (T item in collection)
        {
            if (result == null)
            {
                result = item;
            }
            else
            {
                result += item;
            }
        }
        if (result == null)
        {
            throw new ArgumentOutOfRangeException("Collection is empty");
        }
        return result;
    }

    public static void Main()
    {
        List<int> arr = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
        int min = arr.Min();
        int max = arr.Max();
        int sum = arr.Add();
        Console.WriteLine("{0}, {1}, {2}", min, max, sum);
    }
}
